package dicebot.filene;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import dicebot.abstracts.MessageHandler;
import dicebot.apps.ChatGPT;
import dicebot.apps.LittleGames;
import dicebot.apps.Roll;
import dicebot.apps.Sql;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.BotFactory;
import net.mamoe.mirai.event.GlobalEventChannel;
import net.mamoe.mirai.event.events.GroupMessageEvent;

public class Main {
	
	// To run this project:
	// mvn clean install
	// zip -d target/filene-0.0.1-SNAPSHOT.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF
	// java -jar target/filene-0.0.1-SNAPSHOT.jar
	
	public static final List<MessageHandler> HANDLERS = new ArrayList<>();
	
    public static void main( String[] args ) throws Exception {
    	Sql.initialize();
    	Roll.initialize();
    	ChatGPT.initialize();
    	LittleGames.initialize();
    	
    	Bot bot = BotFactory.INSTANCE.newBot(1379964197l, "filene213");
    	bot.login();

    	GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, new Consumer<GroupMessageEvent>() {
			@Override
			public void accept(GroupMessageEvent event) {
				for (MessageHandler handler : HANDLERS) {
					boolean handled = false;
					try {						
						 handled = handler.handle(event);
					} catch (Exception e) {
						try {
							event.getGroup().sendMessage(e.getMessage());
						} catch (Exception e2) {
						}
					}
					if (handled) {
						break;
					}
				}
			}
		});
    }
}
